package com.huazi.www.dept.servlet;
/*
   User: 王有为
    Date: 2021/5/12
    Time: 16：56
 */


import com.alibaba.fastjson.JSON;
import com.huazi.www.dept.entity.Department;
import com.huazi.www.dept.entity.Department_CT;
import com.huazi.www.dept.service.DepartmentServie;
import com.huazi.www.dept.service.impl.DepartmentServieImpl;
import com.huazi.www.dept.util.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DepartmentServlet extends HttpServlet {
    DepartmentServie departmentServie = new DepartmentServieImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String tp = req.getParameter("tp"); //获取字符
        PrintWriter out = resp.getWriter();//输出流
        if (tp == null) {      //判断字符是否为空
            tp = "select";
        }
        String userName = req.getParameter("userName");//获取姓名文本
        String currentPage = req.getParameter("currentPage");   //获取上一页和下一页
        if (currentPage == null || currentPage.equals("")) {    //判断是否为空
            currentPage = "1";
        }
        if (userName == null) {    //判断用户名是否为空
            userName = "";
        }
        Integer cp = Integer.parseInt(currentPage);     //把获取的分页转换Integer类型
        if (tp.equals("select")) {  //点击查询
            /**
             * 查询待照片
             */
            Page<Department> page = departmentServie.page(userName, cp);    //调用Servie的page方法
            req.setAttribute("userName", userName);      //保存用户名
            req.setAttribute("pageList", page);     //保存对象
            /**
             * 查询已拍照的用户
             */
            Page<Department> pages = departmentServie.pagepaiz(userName, cp);   //调用Servie的pagepaiz方法
            req.setAttribute("pagesList", pages);   //保存对象
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String now = df.format(new Date());// new Date()为获取当前系统时间
            req.setAttribute("time", now);   //保存当前时间
            req.getRequestDispatcher("depa/department.jsp").forward(req, resp); //转发到页面
        }
        //按照编号修改状态
        if (tp.equals("updateid")) {
            String id = req.getParameter("id");     //获取id
            Integer ids = Integer.parseInt(id);         //转换Intget类型
            String state = req.getParameter("state");   //获取是否在排队
            Integer states = Integer.parseInt(state);   //排队转换Integer类型
            boolean modify = departmentServie.modify(ids, states);   //调用Servie的modify方法
            String string = JSON.toJSONString(modify);  //转换JSON格式
            out.print(string);  //输出到页面
            out.flush();      //释放资源
        } else if (tp.equals("ins")) {
            /**
             * 新增CT记录
             */
            String ctNum = req.getParameter("ctNum");   //获取诊断号
            String ctName = req.getParameter("ctName"); //获取用户名
            String ctTime = req.getParameter("ctTime"); //获取时间文本框
            String part = req.getParameter("part");     //获取检查项目
            String situation = req.getParameter("situation");   //获取诊断
            String docId = req.getParameter("docId");   //获取医生名字
            Department_CT ct = new Department_CT(ctNum, ctName, ctTime, part, situation, docId);
            boolean b = departmentServie.insertCt(ct);  //调用Servie的insertCt方法把对象放进去
            String string = JSON.toJSONString(b);   //转换JSON格式
            out.print(string);   //输出到页面
            out.flush();    //释放资源
        }
        out.close();    //关闭流
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
